package com.resume.utils.gen;

import freemarker.template.TemplateException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 梁云亮
 * @Date: 2021/7/16 20:40
 * @Describe:
 */
public class GenApplication {
    private static String className = "Project";
    private static String objName = "project";
    private static String comment = "期日经验";

    private static String basePath = "src/main/java/com/resume/";

    public static void main(String[] args) throws IOException, TemplateException {
        // 生成Service接口
        genService(className, objName, comment);

        //生成Service实现类
        genServiceImpl(className, objName);

        // 生成Controller
        genController(className, objName, comment);
    }

    /**
     * 生成Service接口
     *
     * @param className
     * @param objName
     * @throws IOException
     * @throws TemplateException
     */
    private static void genService(String className, String objName, String comment) throws IOException, TemplateException {
        String ftlNameWithPath = basePath + "utils/gen/ftl/service.ftl";
        String aimFileName = basePath + "service/" + className + "Service.java";
        Map<String, Object> map = new HashMap<>();
        map.put("objName", objName);
        map.put("className", className);
        map.put("comment", comment);
        FreemarkerUtil.execute(ftlNameWithPath, map, aimFileName);
    }

    /**
     * 生成Service实现类
     *
     * @param className
     * @param objName
     * @throws IOException
     * @throws TemplateException
     */
    private static void genServiceImpl(String className, String objName) throws IOException, TemplateException {
        String ftlNameWithPath = basePath + "utils/gen/ftl/serviceImpl.ftl";
        String aimFileName = basePath + "service/impl/" + className + "ServiceImpl.java";
        Map<String, Object> map = new HashMap<>();
        map.put("objName", objName);
        map.put("className", className);
        FreemarkerUtil.execute(ftlNameWithPath, map, aimFileName);
    }

    /**
     * 生成Controller
     *
     * @param className
     * @param objName
     * @throws IOException
     * @throws TemplateException
     */
    private static void genController(String className, String objName, String comment) throws IOException, TemplateException {
        String ftlNameWithPath = basePath + "utils/gen/ftl/controller.ftl";
        String aimFileName = basePath + "controller/" + className + "Controller.java";
        Map<String, Object> map = new HashMap<>();
        map.put("objName", objName);
        map.put("className", className);
        map.put("comment", comment);
        FreemarkerUtil.execute(ftlNameWithPath, map, aimFileName);
    }
}
